<?php

/**
 * Core_Exception 
 * 异常处理类 
 * @uses Exception
 * @package 
 * @version $id$
 * @copyright blankyao
 * @author blankyao <blankyao@gmail.com> 
 * @todo 
 * @license GNU General Public License 2.0
 */
class Core_Exception extends Exception
{
    /**
     *构造函数
     *@param string $message 错误信息
     *@param int $code 错误代码
     */
    public function __construct($message = null, $code = 0)
    {
        parent::__construct($message, $code);
    }

    /**
     *解析错误字符，用友好的形式显示出错误
     *@param string $exceptionString 错误信息
     *@return string $parsedString 解析后的错误信息
     */
    public static function parseError($exceptionString) {
        $exceptionString = trim($exceptionString);
        $rows = explode("\n", $exceptionString);
        $str = '<table width="100%" cellspacing="1" cellpadding="5" border="0" style="background:#777;font-size:8pt;font-family:verdana,Helvetica,sans-serif">';
        $i = 0;

        foreach($rows as $row)
        {
            if(0 == $i)
            {
                $items = explode(' ', $row);
                array_shift($items);
                $message = implode(' ', $items);

                $str .= '<tr><td width=5% style="background:#777;color:#FFF">&nbsp</td><td style="background:#777;color:#FFF">' . $row . '</td></tr>';
            }
            else if(1 == $i)
            {
                $str .= '<tr><td style="background:#FFFFAA"><strong>Trace</strong></td><td align="center" style="background:#FFFFAA">Message</td></tr>';
            }
            else
            {
                $items = explode(' ', $row);
                $num = $items[0];
                array_shift($items);
                $message = implode(' ', $items);

                $str .= "<tr><td style=\"background:#FFF\"><strong>{$num}</strong></td><td style=\"background:#FFF\">{$message}</td></tr>";
            }
            $i ++;
        }

        $str .= '</table>';

        return $str;
    }
    /**
     *打印错误信息
     */
    public function __toString()
    {
            echo '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
                <html><head>
                <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
                <title>Exception</title>
                </head><body><h1 style="font-family:verdana,Helvetica,sans-serif;font-size:12px;background:#AA0000;padding:10px;color:#FFF">'
                . $this->code . ' : ' . $this->message . '</h1>';
            return self::parseError(parent::__toString()) . '</body></html>';
    }

    /**
     *析构函数
     */
    public function __destruct()
    {
    }
}
?>
